package com.fxp.project.jiuselu.web.models.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxp.project.jiuselu.commons.constant.ConstantCode;
import com.fxp.project.jiuselu.commons.exceptions.JiuLuException;
import com.fxp.project.jiuselu.commons.exceptions.ParamException;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.models.goods.entity.JslGoodsAttr;
import com.fxp.project.jiuselu.web.models.goods.mapper.JslGoodsAttrMapper;
import com.fxp.project.jiuselu.web.models.goods.mapper.JslGoodsMapper;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsVo;
import com.fxp.project.jiuselu.web.models.order.entity.JslArea;
import com.fxp.project.jiuselu.web.models.order.entity.JslCart;
import com.fxp.project.jiuselu.web.models.order.entity.JslLogisticArea;
import com.fxp.project.jiuselu.web.models.order.mapper.JslCartMapper;
import com.fxp.project.jiuselu.web.models.order.mapper.JslLogisticAreaMapper;
import com.fxp.project.jiuselu.web.models.order.service.IJslLogisticAreaService;
import com.fxp.project.jiuselu.web.models.order.vo.JslCartVo;
import com.fxp.project.jiuselu.web.models.order.vo.JslLogisticAreaVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 物流地区配置表 服务实现类
 * </p>
 *
 * @author Jason.Wang
 * @since 2021-11-22
 */
@Service
@Log4j2
public class JslLogisticAreaServiceImpl extends ServiceImpl<JslLogisticAreaMapper, JslLogisticArea> implements IJslLogisticAreaService {

    @Autowired
    JslLogisticAreaMapper jslLogisticAreaMapper;
    @Autowired
    JslGoodsMapper jslGoodsMapper;
    @Autowired
    JslCartMapper jslCartMapper;
    @Autowired
    JslGoodsAttrMapper jslGoodsAttrMapper;

    /**
     * selectJslLogisticAreaVoByLogisticIdWithAreaId: 根据当前的地区Id和物流配置Id查询价格信息<br/>
     *
     * @param logisticId
     * @param areaId
     * @return
     * @author Jason.Wang
     * @date 2022/1/27 14:54
     */
    @Override
    public JslLogisticAreaVo selectJslLogisticAreaVoByLogisticIdWithAreaId(String goodsId, Long logisticId, Long areaId, BigDecimal attrWeight, Integer type) {
        BigDecimal money1=new BigDecimal("0.00");
        List<BigDecimal> lis=new ArrayList<>();
        List<JslCart> jslCarts = jslCartMapper.selectList(new QueryWrapper<JslCart>().lambda().eq(JslCart::getUserId, Objects.requireNonNull(BaseController.getUser()).getUserId()));
        JslLogisticAreaVo jslLogisticAreaVo = jslLogisticAreaMapper.selectJslLogisticAreaVoByLogisticIdWithAreaId(logisticId, areaId);

        if (jslLogisticAreaVo == null) {
//            JslLogisticAreaVo jslLogisticAreaVo1 = new JslLogisticAreaVo();
//            jslLogisticAreaVo1.setMoney(new BigDecimal("0.00"));
//            jslLogisticAreaVo1.setWeightprice(attrWeight.toString());
//            return jslLogisticAreaVo1;
            throw new ParamException(ConstantCode.PARAM_EXCEPTION.getCode(), "当前商品不属于配送区域，请联系客服解决");
        }

        //type为1时为购物车支付,type为10时为订单生成页面,对商品数量进行加减后的重量
        // 购物车运费
        if (type == 1) {
            List<String> result = Arrays.asList(goodsId.split(","));
            for (String id : result) {
                GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(Long.valueOf(id));
                logisticId = goodsVo.getLogisticId();
                JslLogisticAreaVo jslLogisticAreaVo1 = jslLogisticAreaMapper.selectJslLogisticAreaVoByLogisticIdWithAreaId(logisticId, areaId);
                jslLogisticAreaVo1.setId(id);
                money1 = getMoney(jslLogisticAreaVo1, result, jslCarts);
                if(goodsVo.getFreeShipping()==1){
                    money1=new BigDecimal("0.00");
                }
//                List<BigDecimal> li = jslLogisticAreaVo1.getLi();
                lis.add(money1);
            }
            BigDecimal sum=lis.stream().reduce(BigDecimal::add).orElse(BigDecimal.valueOf(0));
            System.err.println(sum);
            jslLogisticAreaVo.setMoney(sum);
        } else {
            //立即下单
            String weightprice = jslLogisticAreaVo.getWeightprice();
            JSONArray object = JSON.parseArray(weightprice);
            List<JslArea> list = JSONObject.parseArray(object.toJSONString(), JslArea.class);
            for (JslArea s : list) {
                BigDecimal maxWeight = s.getMaxWeight();
                BigDecimal mixWeight = s.getMixWeight();
                BigDecimal money = s.getMoney();
                if (mixWeight.compareTo(attrWeight) == -1 && maxWeight.compareTo(attrWeight) == 1) {
                    BigDecimal f = attrWeight.multiply(money);
                    jslLogisticAreaVo.setMoney(f);
                } else if (maxWeight.compareTo(attrWeight) == 0) {
                    BigDecimal f = attrWeight.multiply(money);
                    jslLogisticAreaVo.setMoney(f);
                } else if (mixWeight.compareTo(attrWeight) == 0) {
                    BigDecimal f = attrWeight.multiply(money);
                    jslLogisticAreaVo.setMoney(f);
                }
            }
            if (jslLogisticAreaVo.getMoney().compareTo(new BigDecimal("0.00")) == 0) {
                jslLogisticAreaVo.setMoney(list.get(list.size() - 1).getMoney().multiply(attrWeight));
            }
            String id = goodsId.substring(0, goodsId.length());
            String[] result = id.split(",");
            for (String r : result) {
                Long aLong = Long.valueOf(r);
                GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(aLong);
                if (goodsVo.getFreeShipping() == 1) {
                    jslLogisticAreaVo.setMoney(new BigDecimal("0.00"));
                }
            }
        }


//            JslLogisticAreaVo jslLogisticAreaVo = jslLogisticAreaMapper.selectJslLogisticAreaVoByLogisticIdWithAreaId(logisticId, areaId);
//            if (jslLogisticAreaVo == null) {
//                throw new JiuLuException(ConstantCode.PARAM_EXCEPTION.getCode(), "该收货地址暂无法配送,请联系客服");
//            }
//            String weightprice = jslLogisticAreaVo.getWeightprice();
//            JSONArray object = JSON.parseArray(weightprice);
//            List<JslArea> list = JSONObject.parseArray(object.toJSONString(), JslArea.class);
//            for (JslArea s : list) {
//                BigDecimal maxWeight = s.getMaxWeight();
//                BigDecimal mixWeight = s.getMixWeight();
//                BigDecimal money = s.getMoney();
//                if (mixWeight.compareTo(attrWeight) == -1 && maxWeight.compareTo(attrWeight) == 1) {
//                    BigDecimal f = attrWeight.multiply(money);
//                    jslLogisticAreaVo.setMoney(f);
//                } else if (maxWeight.compareTo(attrWeight) == 0) {
//                    BigDecimal f = attrWeight.multiply(money);
//                    jslLogisticAreaVo.setMoney(f);
//                } else if (mixWeight.compareTo(attrWeight) == 0) {
//                    BigDecimal f = attrWeight.multiply(money);
//                    jslLogisticAreaVo.setMoney(f);
//                }
//            }
//            if (jslLogisticAreaVo.getMoney().compareTo(new BigDecimal("0.00"))==0){
//                jslLogisticAreaVo.setMoney(list.get(list.size()-1).getMoney().multiply(attrWeight));
//            }
//            String id = goodsId.substring(0, goodsId.length());
//            String[] result = id.split(",");
//            for (String r : result) {
//                Long aLong = Long.valueOf(r);
//                GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(aLong);
//                if (goodsVo.getFreeShipping() == 1) {
//                    jslLogisticAreaVo.setMoney(new BigDecimal("0.00"));
//                }
//            }

        return jslLogisticAreaVo;
    }

    @Override
    public BigDecimal getMoney(JslLogisticAreaVo jslLogisticAreaVo, List<String> result, List<JslCart> jslCarts) {
//        BigDecimal moneyAll = new BigDecimal("0.00");
        if (jslLogisticAreaVo != null) {
            BigDecimal moneyAll = new BigDecimal("0.00");
            Long userId = BaseController.getUser().getUserId();
            BigDecimal weight1 = new BigDecimal("0.00");
            BigDecimal weight = new BigDecimal("0.00");
            BigDecimal price = new BigDecimal("0.00");
            Long id=Long.valueOf(jslLogisticAreaVo.getId());
//            for (String id : result) {
//                Long aLong = Long.valueOf(id);
//                GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(Long.valueOf(id));
//                logisticId = goodsVo.getLogisticId();
                JslCart jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
            System.out.println(jslCart);
                if (jslCart == null) {
                    throw new JiuLuException(ConstantCode.SYSTEM_EXCEPTION.getCode(), "当前无商品提交,请添加至购物车后重试");
                }
                //防止select语句查询不正确,误删
            jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
            jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
            jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
            jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
            jslCart = jslCartMapper.selectJslCartVoByUserIdAndGoodsId(userId, id);
//                if (!result.contains(jslCart.getGoodsId().toString())){
//                    continue;
//                }
                weight = jslGoodsAttrMapper.selectById(jslCart.getAttrId()).getAttrWeight().multiply(new BigDecimal(jslCart.getGoodsNum()));
//                if (goodsVo.getFreeShipping() == 1) {
//                    continue;
//
            System.err.println("----------->"+weight);
                String weightprice = jslLogisticAreaVo.getWeightprice();
                JSONArray object = JSON.parseArray(weightprice);
                List<JslArea> list = JSONObject.parseArray(object.toJSONString(), JslArea.class);
                for (JslArea s : list) {
                    BigDecimal maxWeight = s.getMaxWeight();
                    BigDecimal mixWeight = s.getMixWeight();
                    BigDecimal money = s.getMoney();
                    if (mixWeight.compareTo(weight) == -1 && maxWeight.compareTo(weight) == 1) {
                        price = weight.multiply(money);
                        jslLogisticAreaVo.setMoney(price);
                    } else if (maxWeight.compareTo(weight) == 0) {
                        price = weight.multiply(money);
                        jslLogisticAreaVo.setMoney(price);
                    } else if (mixWeight.compareTo(weight) == 0) {
                        price = weight.multiply(money);
                        jslLogisticAreaVo.setMoney(weight);
                    }
                }
                if (jslLogisticAreaVo.getMoney().compareTo(new BigDecimal("0.00")) == 0) {
                    price = list.get(list.size() - 1).getMoney().multiply(weight1);
                }
                moneyAll = moneyAll.add(price);
                jslLogisticAreaVo.setMoney(moneyAll);
                return moneyAll;
//            }
////        for (JslCart jslCart : jslCarts) {
//            if (!result.contains(jslCart.getGoodsId().toString())){
//                continue;
//            }
//            Long aLong = jslCart.getGoodsId();
//
//            //单个运费价格
//            price=new BigDecimal("0.00");
//            //计算总量
//            weight=jslGoodsAttrMapper.selectById(jslCart.getAttrId()).getAttrWeight().multiply(new BigDecimal(jslCart.getGoodsNum()));
////            weight1=weight1.add(weight);
//            GoodsVo goodsVo = jslGoodsMapper.selectGoodsVoByGoodsId(aLong);
//            if (goodsVo.getFreeShipping() == 1) {
//                continue;
//            }
//
//            if (jslLogisticAreaVo == null) {
////                throw new JiuLuException(ConstantCode.PARAM_EXCEPTION.getCode(), "该收货地址暂无法配送,请联系客服");
//                return new BigDecimal("0.00");
//            }
////            price=price.add(price);
//
//            String weightprice = jslLogisticAreaVo.getWeightprice();
//            JSONArray object = JSON.parseArray(weightprice);
//            List<JslArea> list = JSONObject.parseArray(object.toJSONString(), JslArea.class);
//            for (JslArea s : list) {
//                BigDecimal maxWeight = s.getMaxWeight();
//                BigDecimal mixWeight = s.getMixWeight();
//                BigDecimal money = s.getMoney();
//                if (mixWeight.compareTo(weight) == -1 && maxWeight.compareTo(weight) == 1) {
//                    price= weight.multiply(money);
//                    jslLogisticAreaVo.setMoney(price);
//                } else if (maxWeight.compareTo(weight) == 0) {
//                    price= weight.multiply(money);
//                    jslLogisticAreaVo.setMoney(price);
//                } else if (mixWeight.compareTo(weight) == 0) {
//                    price= weight.multiply(money);
//                    jslLogisticAreaVo.setMoney(weight);
//                }
//            }
//
//            if (jslLogisticAreaVo.getMoney().compareTo(new BigDecimal("0.00"))==0){
//                price=list.get(list.size()-1).getMoney().multiply(weight1);
//            }
//
//            moneyAll=moneyAll.add(price);
//
//        jslLogisticAreaVo.setMoney(moneyAll);
//
//        return moneyAll;
    }
            return new BigDecimal("0.00");
        }
    }


